package com.demo.woziqi.dao;

import com.demo.woziqi.entity.User;
import com.demo.woziqi.util.GameConstant;
import com.demo.woziqi.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-05-31
 */
public class UserDAO {
    /**
     * 用户注册方法
     *
     * @param user 用户实体
     * @return
     */
    public String getRegister(User user) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String regTime = formatter.format(LocalDateTime.now());
        String sql = "insert into user(username,pwd,regtime) values(?,?,?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setString(1, user.getUsername());
            stat.setString(2, user.getPwd());
            stat.setString(3, regTime);
            // 查询数据库中是否存在该用户名
            if (isUsername(user.getUsername())) {
                return "已存在";
            }
            if (stat.executeUpdate() == 1) {
                GameConstant.username = user.getUsername();
                return "insertOK";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "insertNO";
        }
        return "insertNO";
    }

    /**
     * 通过用户名查询用户的方法
     *
     * @param name 需要查询的注册的用户名
     * @return 返回true 表示数据库中存在用户，返回false 表示数据库中不存在该用户名
     */
    private boolean isUsername(String name) {
        String sql = "select * from user where username=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setString(1, name);
            ResultSet rs = stat.executeQuery();
            return rs.next(); // 有数据返回true,
        } catch (Exception e) {
            e.printStackTrace();
            return false; // 出错返回true,终止插入操作
        }
    }

    /**
     * 用户登录方法
     *
     * @param user 用户
     * @return true 表示登录成功，false表示登录失败
     */
    public User getLogin(User user) {
        String sql = "select * from user where username=? and pwd = ?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement prestat = conn.prepareStatement(sql)) {
            //查询操作,存储在结果集对象中
            prestat.setString(1, user.getUsername());
            prestat.setString(2, user.getPwd());
            ResultSet rs = prestat.executeQuery();
            User u = new User();
            if (rs.next()) { // 返回的有数据,保存信息
                u.setId(rs.getInt("id"));
                u.setPwd(rs.getString("pwd"));
                u.setUsername(rs.getString("username"));
                u.setRegtime(rs.getString("regtime"));
                u.setWin_count(rs.getInt("win_count"));
                u.setLose_count(rs.getInt("lose_count"));
                u.setDraw_count(rs.getInt("draw_count"));
                u.setAll_count(rs.getInt("all_count"));
                u.setIntegral(rs.getInt("integral"));
                return u; // 返回用户信息
            }
            return null; // 数据库无数据 返回null
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 更改用户总场次方法
     *
     * @param user 用户实体类，需要传入需要更新的用户ID
     * @return 返回true，表示更改成功
     */
    public boolean userAllNum(User user) {
        String sql = "UPDATE user SET all_count=all_count+1 WHERE id=?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            if (stat.executeUpdate() == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 查询用户的赢局数，输局数，和棋数，总局数以及剩余积分
     *
     * @param user 用户实体类，需要传入需要更新的用户ID
     * @return 返回查询到的用户user
     */
    public User getNumMsg(User user) {
        String sql = "select win_count,lose_count,draw_count,all_count,integral from user where id =?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            ResultSet rs = stat.executeQuery();
            User u = new User();
            if (rs.next()) { // 返回的有数据,保存信息
                u.setWin_count(rs.getInt("win_count"));
                u.setLose_count(rs.getInt("lose_count"));
                u.setDraw_count(rs.getInt("draw_count"));
                u.setAll_count(rs.getInt("all_count"));
                u.setIntegral(rs.getInt("integral"));
                return u; // 返回用户信息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    /**
     * 胜利方更新赢局以及积分方法
     *
     * @param user 用户实体类，需要传入需要更新的用户ID
     * @return 返回true表示更新成功，返回false表示更新失败
     */
    public boolean userWinNum(User user) {
        String sql = "UPDATE user SET win_count=win_count+1,integral=integral+10 WHERE id=?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            if (stat.executeUpdate() == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 胜利方更新输局以及积分方法
     *
     * @param user 用户实体类，需要传入需要更新的用户ID
     * @return 返回true表示更新成功，返回false表示更新失败
     */
    public boolean userLostNum(User user) {
        String sql = "UPDATE user SET lose_count=lose_count+1,integral=integral-10 WHERE id=?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            if (stat.executeUpdate() == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 胜利方更新和棋方法
     *
     * @param user 用户实体类，需要传入需要更新的用户ID
     * @return 返回true表示更新成功，返回false表示更新失败
     */
    public boolean userStalemateNum(User user) {
        String sql = "UPDATE user SET draw_count=draw_count+1 WHERE id=?";
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (
             PreparedStatement stat = conn.prepareStatement(sql)) {
            stat.setInt(1, user.getId());
            if (stat.executeUpdate() == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
